# Files configure
SRC:=$(wildcard src/*.cpp)
HEAD:=$(wildcard src/*.h)
PCH_X_H:=src/SFC_Inc.hpp
PCH_X:=$(PCH_X_H:src/%.hpp=src/%.hpp.gch)
OBJ:=$(SRC:src/%.cpp=obj/%.o)
DEPS:=$(SRC:src/%.cpp=.deps/%.cpp.d)

# Version configure
TARGET:=ansfc
MAJOR_VER:=0
MINOR_VER:=11
FIX_VER:=3
VERSION:=v$(MAJOR_VER).$(MINOR_VER).$(FIX_VER)

# Release configure
RELEASE_NAME:=$(TARGET)_$(VERSION)
RELEASE_PATH:=../$(RELEASE_NAME)

# Compile arguments configure
CXXFLAGS:=-Wall -g -std=c++1y -D__WORKPATH__=\"`pwd`\" -D__SFC_VERSION__=\"$(VERSION)\"
LDFLAGS:=-Wall -g -lm -lSDL2_ttf -lSDL2 -shared

# Tools configure
BUSYBOX:=$(shell if [ -f ~/busybox.ext ]; then echo "busybox.ext"; else echo ""; fi;)
CP:=$(BUSYBOX) cp
TAR:=$(BUSYBOX) tar
SED:=$(BUSYBOX) sed
AWK:=$(BUSYBOX) awk
RM:=$(BUSYBOX) rm
MKDIR:=$(BUSYBOX) mkdir
ECHO:=$(BUSYBOX) echo
SHELL:=$(BUSYBOX) sh

.PHONY: depend clean cleanpch dist

$(TARGET): obj .deps $(PCH_X) $(OBJ)
	$(CXX) $(LDFLAGS) -o $(TARGET) $(OBJ)

-include $(DEPS)

src/SFC_Inc.hpp.gch: src/SFC_Inc.hpp
	g++ $(CXXFLAGS) -o $@ -x c++-header $<
obj/%.o: src/%.cpp
	$(CXX) $(CXXFLAGS) -Wp,-MD,.deps/$(<F).d -c -o $@ $<
obj .deps:
	$(MKDIR) $@
depend: $(DEPS)
	
.deps/%.cpp.d: src/%.cpp $(HEAD)
	$(RM) -f $@
	$(ECHO) obj/$(shell $(CXX) $(CXXFLAGS) -MM $<) > $@
	$(SED) -i "s/   / /g" $@
clean:
	$(RM) -f $(OBJ) $(TARGET) $(DEPS)
cleanpch:
	$(RM) -f $(PCH_X)
dist: $(DEPS)
	$(RM) -rf $(RELEASE_PATH)
	$(MKDIR) $(RELEASE_PATH) $(RELEASE_PATH)/src $(RELEASE_PATH)/.deps $(RELEASE_PATH)/Font
	$(CP) $(HEAD) $(SRC) $(PCH_X_H) $(RELEASE_PATH)/src/
	$(CP) ./src/.C4droid $(RELEASE_PATH)/src/
	$(CP) $(DEPS) $(RELEASE_PATH)/.deps/
	$(CP) ./Font/* $(RELEASE_PATH)/Font/
	$(CP) ./makefile $(RELEASE_PATH)/
	$(CP) ./Note.txt $(RELEASE_PATH)/
	$(CP) ./.C4droid $(RELEASE_PATH)/
	$(RM) -f $(RELEASE_PATH).tar.gz
	$(TAR) zcvf $(RELEASE_PATH).tar.gz $(RELEASE_PATH)
	$(RM) -rf $(RELEASE_PATH)